﻿  function open_dialog(element,number, parametr,i,object) {//element: window, wall, etc. number: number from main table;if i=1=>remove div
    $("#dialog_" +element+ "_"+number+ "_" +parametr).dialog({width: 1050, title: element + '_' + number});
	//if (i==1){

	/*if (object=='sun'){
	$("#dialog_" +element+ "_"+number+ "_" +parametr ).dialog({width: 1050, title: element + '_' + number, autoOpen: false, close: function(event, ui) { $("#dialog_"+element+ "_"+number+ "_" +parametr).dialog( "destroy"); $("#dialog_"+element+ "_"+number+ "_" +parametr).remove() ; 		solar_irradiance(element,number)  }});
	$("#dialog_" +element+ "_"+number+ "_" +parametr ).dialog('open');
	
	}
	if(object=='radiation'){
		$( "#dialog_" +element+ "_"+number+ "_" +parametr ).dialog({width: 1050, title: element + '_' + number, close: function(event, ui) { $("#dialog_"+element+ "_"+number+ "_" +parametr).dialog( "destroy"); $("#dialog_"+element+ "_"+number+ "_" +parametr).remove() ; 		cooling_load_element(element,number,1)  }});
		}*/

  //}
  }

  
  function expand_table(Event) {
var zzakk=Event.target||event.srcElement;;
var zakr=zzakk.className;

  if (zakr=="see") {
     for (var i=0;i<zzakk.childNodes.length;i++) {
             if (zzakk.childNodes[i].className=="notsee") {
                zzakk.childNodes[i].className="nowsee";
             }
			 else if (zzakk.childNodes[i].className=="nowsee") {
                zzakk.childNodes[i].className="notsee";
             }

        }
  }
  
}

var CTS = new Array();
var CTS_table_print;
var CTS_table = new Array();
var CTS_array_table = new Array();
var CTS_array_calc = new Array();
var collapse_table;
CTS_titles_1 = ";;;;;;Conductive Time Factors,%;;;;;;;;;;;;;;;;;;;;;;;;Layer ID from outside to inside;;;;;;;";
CTS_titles_2 = ";Wall number;U-Factor,(W/m2*K);Total R;Mass,kg/m2;Thermal Capacity,kJ/(m2*K);0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;;;;;;;;";
CTS_ASHRAE = "CURTAIN_WALLS 1 0.428 2.3 31.0 30.7 18 58 20 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F09 F04 I02 F04 G01 F02 0;CURTAIN_WALLS 2 0.429 2.3 20.9 20.4 25 57 15 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F08 F04 I02 F04 G01 F02 0;CURTAIN_WALLS 3 0.428 2.3 80.0 67.5 8 45 32 11 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F10 F04 I02 F04 G01 F02 0;STUD_WALLS 4 0.419 2.4 25.5 24.5 19 59 18 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F08 G03 I04 G01 F02 0 0;STUD_WALLS 5 0.417 2.4 84.6 73.6 6 42 33 13 4 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F10 G03 I04 G01 F02 0 0;STUD_WALLS 6 0.406 2.5 25.6 32.7 7 44 32 12 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F11 G02 I04 G04 F02 0 0;STUD_WALLS 7 0.413 2.4 66.7 61.3 5 41 34 13 4 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F07 G03 I04 G01 F02 0 0;EIFS 8 0.668 1.5 36.6 36.7 11 50 26 9 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F06 I01 G03 F04 G01 F02 0;EIFS 9 0.305 3.3 38.3 38.8 2 25 31 20 11 5 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F01 F06 I01 G03 I04 G01 F02 0;EIFS 10 0.524 1.9 130.9 120.6 1 2 6 9 9 9 8 7 6 6 5 5 4 4 3 3 3 2 2 2 2 1 1 0 F01 F06 I01 G03 M03 F04 G01 F02";
CTS_titles_1_array = CTS_titles_1.split(";");//split array of titles 1st line
CTS_titles_2_array = CTS_titles_2.split(";");//split array of titles 2nd line
CTS_array = CTS_ASHRAE.split(";"); //split array to string by ";"
for (var i=0; i<CTS_array.length; i++) { //split strings to elements by " "
CTS_array_table[i] = CTS_array[i].split(" ");
}


function create_titles_CTS() {
   CTS_titles_table = "";
   CTS_titles_1_table = "";
   CTS_titles_2_table = "";
for (var j=1; j<CTS_array_table[0].length; j++) {	 
	 /////prepare titles
	 if (j==1){
	 CTS_titles_1_table = CTS_titles_1_table + "<td colspan = 5><b>" + CTS_titles_1_array[j+1] + "</b></td>";
	 }
	  else if (j==7){
	 CTS_titles_1_table = CTS_titles_1_table + "<td colspan = 24 align = center><b>" + CTS_titles_1_array[j-1] + "</b></td>";
	 }
	  else if (j==29){
	 CTS_titles_1_table = CTS_titles_1_table + "<td colspan = 8  align = center><b>" + CTS_titles_1_array[j+1] + "</b></td>";
	 }
	  
	 CTS_titles_2_table = CTS_titles_2_table + "<td><b>" + CTS_titles_2_array[j] + "</b></td>";
	 }
		CTS_titles_table = CTS_titles_table + "<tr>" + CTS_titles_1_table + "<tr></tr>" + CTS_titles_2_table + "</tr>";
		CTS_titles_table = CTS_titles_table + "</tr>";
		return CTS_titles_table;
}
		
		
		
  function generate_table_CTS(element,number,parametr) {
   CTS_table_print = "";
   for (var i=0; i<CTS_array.length; i++) {
   CTS_table[i] = "<tr>";
   
     for (var j=1; j<CTS_array_table[i].length; j++) {
	 CTS_table[i] = CTS_table[i] + "<td align=center><h4 style=cursor:hand onClick=select_from_base('"+ element+ "','" + i + "'" + "," + "'" + number + "','" + parametr + "')>" + CTS_array_table[i][j] + "</h4></td>";
	 
	 
	 /////prepare titles

	 ////////
	 }
	 CTS_table[i] = CTS_table[i] + "</tr>";
	 
	if (i == 0) {
    CTS_table_print = "<ul> <ul class=see onClick=expand_table(event)>" + CTS_array_table[i][0] + "<ul class=notsee><table>"  + create_titles_CTS() + CTS_table[i] + "</ul></ul>";
   }

	 if (i != 0 && CTS_array_table[i][0] == CTS_array_table[i-1][0]) {
	 CTS_table_print = CTS_table_print + CTS_table[i];
		if (!CTS_array_table[i+1]){ 
		CTS_table_print = CTS_table_print + "</table></ul></ul>"
		}
		else if (CTS_array_table[i][0] != CTS_array_table[i+1][0]){
		CTS_table_print = CTS_table_print + "</table></ul></ul>"
		}
	 }
	 else if(i != 0){
	 CTS_table_print = CTS_table_print + "<ul class=see onClick=expand_table(event)>" + CTS_array_table[i][0] + "<ul class=notsee><table>" +create_titles_CTS() + CTS_table[i];
		if (!CTS_array_table[i+1]){
		CTS_table_print = CTS_table_print + "</table></ul></ul>"
		}
		else if (CTS_array_table[i][0] != CTS_array_table[i+1][0]){
		CTS_table_print = CTS_table_print + "</table></ul></ul>"
		}
		}
	 }//end for
	 
	 

document.getElementById(element + "_"+number+"_CTS").innerHTML = CTS_table_print + "</ul>";
open_dialog(element,number, parametr);
}

                                  //element_number - number of selected wall
function select_from_base(element,element_number,number,parametr){ //select string elements from table and assign this string to CTS_number_element variable
/*for (var j=0; j<CTS_array_table[number].length; j++) {
CTS[j] = CTS_array_table[number][j];
}
CTS.splice(0,6);*/

document.getElementById("replace_" +element +"_" +number+ "_CTS").innerHTML = "" + CTS_array_table[element_number][0] + CTS_array_table[element_number][1];
$("#dialog_"+element+ "_"+number+ "_" +parametr).dialog( "destroy" );
}

function read_from_base_CTS(element,number) {
  data = document.getElementById("replace_" +element +"_" +number+ "_CTS").innerHTML;
  var CTS_string = "";
  for (var i=0; i<CTS_array.length; i++) {
    if (CTS_array_table[i][0] + CTS_array_table[i][1] == data) {
	for (var j=6; j<30; j++) {
    CTS_string += CTS_array_table[i][j] + " ";	
	};
	return CTS_string;
    break;
	}
	}
}
